<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="输入活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
            v-model="selectDate"
            type="daterange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button @click="addOrUpdateHandle()" v-if="isAdmin()">新建活动</el-button>
        
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
      <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="活动名称">
      </el-table-column>
      <el-table-column
          prop="startTime"
          header-align="center"
          align="center"
          label="开始时间">
      </el-table-column>
      <el-table-column
          prop="endTime"
          header-align="center"
          align="center"
          label="结束时间">
      </el-table-column>
      <el-table-column
          prop="peopleNum"
          header-align="center"
          align="center"
          label="参与人数">
      </el-table-column>
      <el-table-column
          prop="duringTime"
          header-align="center"
          align="center"
          label="活动时长">
      </el-table-column>
      <el-table-column
          prop="allTime"
          header-align="center"
          align="center"
          label="总志愿时长">
      </el-table-column>
      <el-table-column
          prop="fellowName"
          header-align="center"
          align="center"
          label="活动负责人">
      </el-table-column>
      <el-table-column
          prop="fellowTel"
          header-align="center"
          align="center"
          label="电话">
      </el-table-column>
      <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="250"
          label="操作">
        <template v-slot:default="scope">
          <el-button v-if="scope.row.buttonType==1" type="primary" size="small" @click="toInformation(scope.row.id)">完善资料</el-button>
          <el-button v-if=isAdmin() type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button v-if=isAdmin() type="primary" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <div style="text-align: right;">总志愿时长：{{ allTime }}</div>
    
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import store from '@/store';
import AddOrUpdate from './activity-add-or-update'
import service from "@/utils/httpRequest";
// import vuex

export default {
  data() {
    return {
      selectDate: [],
      dataForm: {
        key: ''
      },
      allTime:'',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
      this.getDataList()
    },
  mounted() {
    this.getDataList();
  },
  methods: {
    //跳转到完善资料页面
    toInformation(id) {
      this.$router.push({
        path: '/home/information',
        query: {
          id: id
        }
      })
    },
    // 获取总活动时长
    getAllTime() {
      service({
        method: 'GET',
        url: '/lovehelp/activity/allactivitytime',
        params: {
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          startTime: this.selectDate ? this.selectDate[0] : '',
          endTime: this.selectDate ? this.selectDate[1] : ''
        }
      }).then(res => {
        if (res.code === 200) {
          this.allTime = res.time;
        } else {
          this.allTime = '';
        }
      })
    },
    // 获取数据列表
    getDataList() {
      this.getAllTime()
      this.dataListLoading = true
      service({
        method: 'GET',
        url: '/lovehelp/activity/list',
        params: {
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          startTime: this.selectDate ? this.selectDate[0] : '',
          endTime: this.selectDate ? this.selectDate[1] : ''
        }
      }).then(res => {
        if (res.code === 200) {
          this.dataList = res.page.list;
          this.totalPage = res.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?该操作将删除该活动所有活动记录。`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        service({
          method: 'POST',
          url: '/lovehelp/activity/delete',
          data: ids
        }).then(res => {
          if (res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message.info("已取消删除");
      })
    },
    isAdmin() {
      console.log("活动页面获取用户信息",this.$store.state.user.loginUser)
      return this.$store.state.user.loginUser.role===1 || store.state.user.loginUser.role===2
    }
  }
}
</script>
